package com.qianchang.microfinance.view.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.dm.library.widgets.element.DTextView;

import java.util.ArrayList;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * Created by tangjian on 2016/8/25.
 */
public abstract class BaseFragment extends Fragment {

    protected View titleToolbar;
    protected View mView;
    protected Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = onRootViewCreate(inflater, container, savedInstanceState);
        initView();
        return mView;
    }

    protected abstract View onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListener();
        initData();
    }

    /**
     * 设置页面Title
     */
    public void setTitle(CharSequence title) {
        if (titleToolbar != null) {
            ((TextView) titleToolbar.findViewById(R.id.tv_title)).setText(title);
        }
    }

    /**
     * 设置页面Title
     */
    public void setTitle(int titleId) {
        if (titleToolbar != null) {
            ((TextView) titleToolbar.findViewById(R.id.tv_title)).setText(titleId);
        }
    }


    /**
     * 设置页面左边图片
     */
    public void setLeftContent(String leftContent, int imgId) {
        if (titleToolbar != null) {
            DTextView tvBack = (DTextView) titleToolbar.findViewById(R.id.tv_back);
            tvBack.setTextContent(leftContent);
            Drawable drawableLeft = ResourcesCompat.getDrawable(getResources(), imgId, null);
            if (null != drawableLeft) {
                drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());//必须设置图片大小，否则不显示
                tvBack.setCompoundDrawables(drawableLeft, null, null, null);
            }
            tvBack.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏页面左边图片
     */
    public void hideLeftContent() {
        if (titleToolbar != null) {
            DTextView tvBack = (DTextView) titleToolbar.findViewById(R.id.tv_back);
            tvBack.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置页面右边图片
     *
     * @param imgId
     */
    public void setRightContent(String rightContent, int imgId) {
        if (titleToolbar != null) {
            DTextView tvRight = (DTextView) titleToolbar.findViewById(R.id.tv_right);
            tvRight.setTextContent(rightContent);
            Drawable rightDrawable = ResourcesCompat.getDrawable(getResources(), imgId, null);
            if (null != rightDrawable) {
                rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
                tvRight.setCompoundDrawables(null, null, rightDrawable, null);
            }
            tvRight.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏页面右边图片
     */
    public void hideRightContent() {
        if (titleToolbar != null) {
            titleToolbar.findViewById(R.id.tv_right).setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置页面右边第二个图片
     *  2017.12.14 鸿胜科技新曾的
     */
    public void setRightTwoContent(String rightContent, int imgId) {
        if (titleToolbar != null) {
            DTextView tvRight = (DTextView) titleToolbar.findViewById(R.id.tv_right_two);
            tvRight.setTextContent(rightContent);
            Drawable rightDrawable = ResourcesCompat.getDrawable(getResources(), imgId, null);
            if (null != rightDrawable) {
                rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
                tvRight.setCompoundDrawables(null, null, rightDrawable, null);
            }
            tvRight.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏页面右边第二个图片
     * 2017.12.14 鸿胜科技新曾的
     *
     */
    public void hideRightTwoContent() {
        if (titleToolbar != null) {
            titleToolbar.findViewById(R.id.tv_right_two).setVisibility(View.INVISIBLE);
        }
    }

    /***
     *  设置页面左边文字
     *  2017.12.14 鸿胜科技新曾的
     */
    public void setLeftPhone(CharSequence phone){
        if (titleToolbar != null) {
            ((LinearLayout) titleToolbar.findViewById(R.id.title_left_linear_id)).setVisibility(View.VISIBLE);
            ((TextView) titleToolbar.findViewById(R.id.tv_title_left_id)).setText(phone);
        }
    }


    /**
     *  获取左边头像 ImgView
     *  2017.12.14 鸿胜科技新曾的
     */
    public ImageView getLeftHeadImg(){
        ImageView headImg = null;
        if (titleToolbar != null) {
            ((LinearLayout) titleToolbar.findViewById(R.id.title_left_linear_id)).setVisibility(View.VISIBLE);
            headImg = (ImageView) titleToolbar.findViewById(R.id.left_head_img);
        }
        return headImg;
    }

    /**
     *  获取左边头像 ImgView 显示
     *  2017.12.14 鸿胜科技新曾的
     */
    public void setLeftLineaVisible(){
        ImageView headImg = null;
        if (titleToolbar != null) {
            ((LinearLayout) titleToolbar.findViewById(R.id.title_left_linear_id)).setVisibility(View.VISIBLE);
            headImg = (ImageView) titleToolbar.findViewById(R.id.left_head_img);
        }
    }


    /**
     * 检查是否可执行点击操作 防重复点击
     *
     * @return 返回true则可执行
     */
    public boolean checkClick(int id) {
        BaseActivity activity = (BaseActivity) getActivity();
        return activity.checkClick(id);
    }

    protected void initView() {
        titleToolbar = mView.findViewById(R.id.title_bar);
        if (null != titleToolbar) {
            View back = titleToolbar.findViewById(R.id.tv_back);
            back.setVisibility(View.GONE);
        }
    }

    public void initData() {
    }

    protected void initListener() {

    }

    public void startActivity(Class clazz) {
        Intent intent = new Intent();
        intent.setClass(mContext, clazz);
        startActivity(intent);
    }

    public void startActivity(Class clazz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mContext, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    /**
     * 含有Bundle通过Class跳转界面,并判断是否销毁当前界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle, boolean finish) {
        Intent intent = new Intent();
        intent.setClass(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        if (finish) {
            ((Activity) mContext).finish();
        }
    }

    public void startActivity(Class clazz, String stringData) {
        Intent intent = new Intent();
        intent.setClass(mContext, clazz);
        if (stringData != null) {
            intent.putExtra(ConstantAppInfo.STRING_DATA, stringData);
        }
        startActivity(intent);
    }


    public void startActivity(Class clazz, String stringData, boolean finish) {
        Intent intent = new Intent();
        intent.setClass(mContext, clazz);
        if (stringData != null) {
            intent.putExtra(ConstantAppInfo.STRING_DATA, stringData);
        }
        startActivity(intent);
        if (finish) {
            ((Activity) mContext).finish();
        }
    }


    protected boolean checkCommitAble(ArrayList<Boolean> commitList) {
        for (Boolean aBoolean : commitList) {
            if (!aBoolean) {
                return false;
            }
        }
        return true;
    }

    protected ArrayList<Boolean> initCommitList(int size) {
        ArrayList<Boolean> booleanList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            booleanList.add(false);
        }
        return booleanList;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    public void dismiss() {
        if (null != mContext && mContext instanceof DmBaseActivity) {
            ((DmBaseActivity) mContext).dismiss();
        }
    }

    public void show() {
        if (null != mContext && mContext instanceof DmBaseActivity) {
            ((DmBaseActivity) mContext).show();
        }
    }

    public void show(boolean cancelable) {
        if (null != mContext && mContext instanceof DmBaseActivity) {
            ((DmBaseActivity) mContext).show(cancelable);
        }
    }


}
